package com.bayair.doctor.ui.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import com.bayair.doctor.R;
import com.bayair.doctor.biz.helper.SpSetting;
import com.bayair.doctor.mvp.BaseMVPActivity;
import com.bayair.doctor.mvp.annotation.InjectPresenter;
import com.bayair.doctor.ui.main.MainActivity;
import com.bayair.doctor.utils.SimpleAnimUtils;
import com.example.library.widget.button.loadingbutton.LoadingButton;
import com.example.library.widget.edittext.ClearEditText;
import com.example.library.widget.edittext.PhoneEditText;
import com.example.library.widget.textview.CountdownView;

import androidx.constraintlayout.widget.ConstraintLayout;
import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseMVPActivity implements LoginContract.View {

    @BindView(R.id.et_phone)
    PhoneEditText mEtPhone;
    @BindView(R.id.et_verify)
    ClearEditText mEtVerify;
    @BindView(R.id.cd_btn_verify2)
    CountdownView mCDBtnVerify2;
    @BindView(R.id.cb_include_agree)
    CheckBox mCheck;

    @BindView(R.id.cl_login_root)
    ConstraintLayout mClLoginRoot;

    @BindView(R.id.btn_login)
    LoadingButton mLoginButton2;
    @BindView(R.id.ll_include_container)
    LinearLayout mLlBottom;

    @InjectPresenter
    LoginPresenter mPresenter;

    @Override
    public boolean statusBarDarkFont() {
        return true;
    }

    @Override
    protected boolean isNeedAddIMEventListener() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {

    }

    @Override
    protected void initData() {
        //初始化sp中保存的phone
        if (!TextUtils.isEmpty(SpSetting.getPhone())) {
            mEtPhone.setText(SpSetting.getPhone());
        }
    }

    @Override
    protected void setListener() {
        mLoginButton2.setOnLoadingListener(new LoadingButton.OnLoadingListenerAdapter() {
            @Override
            public void onCompleted() {

            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isFinishing()) {
            mLoginButton2.cancel();
        }
    }

    @OnClick(R.id.cd_btn_verify)
    public void verify() {
        mPresenter.verity();
    }

    @OnClick(R.id.cd_btn_verify2)
    public void verify2() {
        mPresenter.verity();
    }

    @OnClick(R.id.btn_login)
    public void login2() {
        mPresenter.login();
    }

    @OnClick(R.id.tv_include_user)
    public void userProtocol() {
        ProtocolActivity.actionStart(this,"用户协议");
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }

    @OnClick(R.id.tv_include_privacy)
    public void privacyProtocol() {
        ProtocolActivity.actionStart(this,"隐私政策");
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }

    @Override
    public String getPhoneNum() {
        return mEtPhone.getPhoneText().trim();
    }

    @Override
    public String getAuthCode() {
        return mEtVerify.getText().toString().trim();
    }

    @Override
    public boolean isCheckAgree() {
        return mCheck.isChecked();
    }

    @Override
    public void resetVerity() {
        mCDBtnVerify2.resetState();
    }

    @Override
    public void loginSuccess() {
        mLoginButton2.complete();

        startActivity(MainActivity.class);
        finish();
    }

    @Override
    public void loginFail(String msg) {
        mLoginButton2.fail();
    }

    @Override
    public void notifyNoCheck() {
        toast("请勾选同意协议");
        mLlBottom.startAnimation(SimpleAnimUtils.shakingAnimation());
    }

    @Override
    public void showLoading() {
        mLoginButton2.start();
    }
}
